<?php
namespace Home\Model;
use Think\Model;
class GoodsModel extends Model {


    /*商品收藏记录*/
    public function getCollectionList($p=array()){
        $list = $data = array();
        $p['status'] = 1;
        $data = M('collection')->where($p)->order('addtime desc')->limit('0,6')->select();
        if($data){
            foreach($data as $k=>$v){
                $list[$k] = $this->field('id,product_name,market_price,shop_price,img1')->find($v['goods_id']);
                $list[$k]['addtime'] = $v['addtime'];
                $list[$k]['cid'] = $v['cid'];
            }
        }
        return $list;
    }
//add
    public function addCollection($uid,$goods_id,$p=array(),$checkR=''){
        $cModel = M('collection');
        $p['uid'] = $uid;
        $p['goods_id'] = $goods_id;
        $addtime = time();
        $checkR = $cModel->where($p)->getField('cid');
        if(!$checkR){
            $p['addtime'] = $addtime;
            $cModel->add($p);
            return ['s'=>true,'msg'=>'收藏成功'];
        }else{
            return ['s'=>false,'msg'=>'该商品已收藏过'];
        }
    }
//    del
    public function delCollection($uid,$cid){
        $cModel = M('collection');
        $p = ['uid'=>$uid,'cid'=>$cid,'status'=>2];
        $cModel->save($p);
        $s = $cModel->getLastSql();
        return $cModel->save($p);
//        return $s;
    }
    /*商品浏览记录*/
    public function getFootPrintList($p=array()){
        $list = $data = array();
        $data = M('footprint')->where($p)->order('addtime desc')->limit('0,6')->select();
        if($data){
           foreach($data as $k=>$v){
               $list[$k] = $this->field('id,product_name,market_price,shop_price,img1')->find($v['goods_id']);
               $list[$k]['addtime'] = $v['addtime'];
           }
        }
        return $list;
    }
//add or update
    public function addFootPrint($uid,$goods_id,$p=array()){
        $fpModel = M('footprint');
        $p['uid'] = $uid;
        $p['goods_id'] = $goods_id;
        $addtime = time();
        $checkR = $fpModel->where($p)->getField('fp_id');
        if(!$checkR){
            $p['addtime'] = $addtime;
            $r = $fpModel->add($p);
        }else{
            $s['addtime'] = $addtime;
            $s['fp_id'] = $checkR;
            $r = $fpModel->save($s);
        }
        return $r;
    }
}
